/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.bank.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.api.R;
import org.springblade.modules.bank.entity.TmSelectSql;
import org.springblade.modules.bank.service.ITmSelectSqlService;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 查询配置sql 控制器
 *
 * @author Blade
 * @since 2022-07-08
 */
@RestController
@AllArgsConstructor
@RequestMapping("/tmSelectSql")
@Api(value = "查询配置sql", tags = "查询配置sql")
public class TmSelectSqlController extends BladeController {

	private ITmSelectSqlService tmSelectSqlService;


	/**
	 * 根据信息查询数据
	 * @param keyName sql编码
	 * @param sql 所执行sql
	 * @param param sql执行所需参数 参数格式为json字符串 {"p1":"-1","p2":"20220719001"}
	 * @return
	 */
	@PostMapping("/getInfo")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "查询配置sql", notes = "传入TmSelectSql")
	public R<List<JSONObject>> detail(String keyName, String sql, String param) {
		List<JSONObject> resList = new ArrayList<>();
		TmSelectSql detail = tmSelectSqlService.getOne(new QueryWrapper<TmSelectSql>()
				.lambda()
				.eq(TmSelectSql::getKeyName,keyName));
		if(null == detail){
			return R.fail("请在tm_select_sql表中配置key值为"+keyName+"的sql!");
		}
		String strSql = "";
		if(sql.equals("sql1")){
			strSql = detail.getSql1();
		}else if(sql.equals("sql2")){
			strSql = detail.getSql2();
		}else if(sql.equals("sql3")){
			strSql = detail.getSql3();
		}else if(sql.equals("sql4")){
			strSql = detail.getSql4();
		}else if(sql.equals("sql5")){
			strSql = detail.getSql5();
		}else if(sql.equals("sql6")){
			strSql = detail.getSql6();
		}else if(sql.equals("sql7")){
			strSql = detail.getSql7();
		}else if(sql.equals("sql8")){
			strSql = detail.getSql8();
		}else if(sql.equals("sql9")){
			strSql = detail.getSql9();
		}else if(sql.equals("sql10")){
			strSql = detail.getSql10();
		}else if(sql.equals("sql11")){
			strSql = detail.getSql11();
		}else if(sql.equals("sql12")){
			strSql = detail.getSql12();
		}else{
			return R.fail("请传入sql!");
		}
		if(!"".equals(strSql)){
			if(null!=param){
				JSONObject paramObj = JSON.parseObject(param);
				if(paramObj.containsKey("p1")){
					System.out.println("有p1参数");
					strSql = strSql.replace("$p1",paramObj.get("p1").toString());
				}
				if(paramObj.containsKey("p2")){
					System.out.println("有p2参数");
					strSql = strSql.replace("$p2",paramObj.get("p2").toString());
				}
				if(paramObj.containsKey("p3")){
					System.out.println("有p3参数");
					strSql = strSql.replace("$p3",paramObj.get("p3").toString());
				}
				if(paramObj.containsKey("p4")){
					System.out.println("有p4参数");
					strSql = strSql.replace("$p4",paramObj.get("p4").toString());
				}
				if(paramObj.containsKey("p5")){
					System.out.println("有p5参数");
					strSql = strSql.replace("$p5",paramObj.get("p5").toString());
				}
				if(paramObj.containsKey("p6")){
					System.out.println("有p6参数");
					strSql = strSql.replace("$p6",paramObj.get("p6").toString());
				}
				if(paramObj.containsKey("p7")){
					System.out.println("有p2参数");
					strSql = strSql.replace("$p7",paramObj.get("p7").toString());
				}
				if(paramObj.containsKey("p8")){
					System.out.println("有p2参数");
					strSql = strSql.replace("$p8",paramObj.get("p8").toString());
				}
				if(paramObj.containsKey("p9")){
					System.out.println("有p9参数");
					strSql = strSql.replace("$p9",paramObj.get("p9").toString());
				}
				if(paramObj.containsKey("p10")){
					System.out.println("有p10参数");
					strSql = strSql.replace("$p10",paramObj.get("p10").toString());
				}
			}
			resList = tmSelectSqlService.executeSQL(strSql);
		}else{
			return R.fail("请在tm_select_sql表中配置key为"+keyName+"的sql!");
		}
		return R.data(resList);
	}
}
